package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbMutualInspectiondetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbSpecialInspectiondetailBo;
import com.ruoyi.tb.lmt.domain.vo.TbSpecialInspectiondetailVo;
import com.ruoyi.tb.lmt.domain.TbSpecialInspectiondetail;
import com.ruoyi.tb.lmt.mapper.TbSpecialInspectiondetailMapper;
import com.ruoyi.tb.lmt.service.ITbSpecialInspectiondetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 专检详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@RequiredArgsConstructor
@Service
public class TbSpecialInspectiondetailServiceImpl implements ITbSpecialInspectiondetailService {

    private final TbSpecialInspectiondetailMapper baseMapper;

    @Autowired
    private TbSpecialInspectiondetailMapper tbSpecialInspectiondetailMapper;

    /**
     * 查询专检详情
     */
    @Override
    public TbSpecialInspectiondetailVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询专检详情列表
     */
    @Override
    public TableDataInfo<TbSpecialInspectiondetailVo> queryPageList(TbSpecialInspectiondetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbSpecialInspectiondetail> lqw = buildQueryWrapper(bo);
        Page<TbSpecialInspectiondetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询专检详情列表
     */
    @Override
    public List<TbSpecialInspectiondetailVo> queryList(TbSpecialInspectiondetailBo bo) {
        LambdaQueryWrapper<TbSpecialInspectiondetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbSpecialInspectiondetail> buildQueryWrapper(TbSpecialInspectiondetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbSpecialInspectiondetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMutualId() != null, TbSpecialInspectiondetail::getMutualId, bo.getMutualId());
        lqw.eq(bo.getWorkCategoryId() != null, TbSpecialInspectiondetail::getWorkCategoryId, bo.getWorkCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCategory()), TbSpecialInspectiondetail::getWorkCategory, bo.getWorkCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCategoryRemark()), TbSpecialInspectiondetail::getWorkCategoryRemark, bo.getWorkCategoryRemark());
        lqw.like(StringUtils.isNotBlank(bo.getWorkName()), TbSpecialInspectiondetail::getWorkName, bo.getWorkName());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckReapirReqire()), TbSpecialInspectiondetail::getCheckReapirReqire, bo.getCheckReapirReqire());
        lqw.eq(bo.getStartTime() != null, TbSpecialInspectiondetail::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, TbSpecialInspectiondetail::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFinshSituation()), TbSpecialInspectiondetail::getFinshSituation, bo.getFinshSituation());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbSpecialInspectiondetail::getWorkGroup, bo.getWorkGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser()), TbSpecialInspectiondetail::getWorkUser, bo.getWorkUser());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckUser()), TbSpecialInspectiondetail::getCheckUser, bo.getCheckUser());
        lqw.eq(bo.getCheckTime() != null, TbSpecialInspectiondetail::getCheckTime, bo.getCheckTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbSpecialInspectiondetail::getStatus, bo.getStatus());
        lqw.eq(bo.getCreateTime() != null, TbSpecialInspectiondetail::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbSpecialInspectiondetail::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbSpecialInspectiondetail::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbSpecialInspectiondetail::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser2()), TbSpecialInspectiondetail::getWorkUser2, bo.getWorkUser2());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkKind()), TbSpecialInspectiondetail::getWorkKind, bo.getWorkKind());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskUrl()), TbSpecialInspectiondetail::getTaskUrl, bo.getTaskUrl());
        return lqw;
    }

    /**
     * 新增专检详情
     */
    @Override
    public Boolean insertByBo(TbSpecialInspectiondetailBo bo) {
        TbSpecialInspectiondetail add = BeanUtil.toBean(bo, TbSpecialInspectiondetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改专检详情
     */
    @Override
    public Boolean updateByBo(TbSpecialInspectiondetailBo bo) {
        TbSpecialInspectiondetail update = BeanUtil.toBean(bo, TbSpecialInspectiondetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbSpecialInspectiondetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除专检详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TbSpecialInspectiondetailVo> selectSpecial(Long mutualId, String lmtType, String lmtNumber, String status, PageQuery pageQuery) {
        List<TbSpecialInspectiondetailVo> result = tbSpecialInspectiondetailMapper.selectSpecial(mutualId,lmtType,lmtNumber,status);
        //分页
        if (pageQuery.getPageSize()!= null && pageQuery.getPageNum()!= null) {
            List<TbSpecialInspectiondetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }
}
